const fs = require('fs')

module.exports = {
	controller: null,
	path: '',
	init: function(path, controller) {
		if (!controller) {
			console.error("参数 controller 未设置")
			return false;
		}
		this.controller = controller
		this.path = path ? path : this.path
		this.listDir(this.path)
	},

	listDir: function(dir) {
		var fileList =fs.readdirSync(dir, 'utf-8')

		for (var i = 0; i < fileList.length; i++) {

			if (fileList[i] == 'index.js') continue

			var stat = fs.lstatSync(dir + fileList[i]);
			if (stat.isDirectory()) {
				this.listDir(dir + fileList[i] + '/');
			} else {
				this.loadRoute(dir + fileList[i]);
			}
		}
	},

	loadRoute: function(routeFile) {
		let path = '..' + routeFile.substring(1);
		const _path = path.substring(13);
		require(path)(_path, this.controller)
	}

}